<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/29
 * Time: 10:09
 */

namespace app\admin\controller;
use app\admin\model\FeedbackModel;

class Feedback extends Base
{
    // 留言列表
    public function index()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['content'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $feedback = new FeedbackModel();
            $selectResult = $feedback->getFeedbackByWhere($where, $offset, $limit,1);
            foreach($selectResult as $key=>&$vo){
                $selectResult[$key]['status'] = reply_status($vo['status']);
                $selectResult[$key]['addtime'] = date('Y-m-d H:i:s',$vo['addtime']);
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }

            $return['total'] = $feedback->getAllFeedback($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        $param=input('param.');
        $data=array('user_id'=>0);
        if(isset($param['user_id'])){
            $data['user_id']=$param['user_id'];
        }
        $data['sch']=json_encode(array(
            array('id'=>'searchText','name'=>'意见反馈'),
        ));
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
    public function reply(){
        $param=input('param.');
        $feedback = new FeedbackModel();
        if(request()->isAjax()){
            $check=paramCheck($param,'id,reply');
            if($check['code']!=1) return json($check);
            return json($feedback->reply($param));
        }
        $data=$feedback->getOneFeedback($param['id']);
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '回复' => [
                'auth' => 'feedback/reply',
                //'href' => url('feedback/reply', ['id' => $id]),
                'href' => "javascript:reply(" . $id . ")",
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ]
        ];
    }
}